// pages/city/city.js
import citylist from "./citylist";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timer: null,
    firstTime: true,
    getAddressFlag: true,
    hotcity: [{
      cityName: "苏州"
    }, {
      cityName: "南京"
    }, {
      cityName: "无锡"
    }, {
      cityName: "徐州"
    }, {
      cityName: "常州"
    }, {
      cityName: "南通"
    }, {
      cityName: "淮安"
    }, {
      cityName: "盐城"
    }, {
      cityName: "扬州"
    }, {
      cityName: "镇江"
    }, {
      cityName: "泰州"
    }, {
      cityName: "宿迁"
    }, {
      cityName: "连云港"
    }, ],
    cityAZ: [{
      cityName: 'A'
    }, {
      cityName: 'B'
    }, {
      cityName: 'C'
    }, {
      cityName: 'D'
    }, {
      cityName: 'E'
    }, {
      cityName: 'F'
    }, {
      cityName: 'G'
    }, {
      cityName: 'H'
    }, {
      cityName: 'J'
    }, {
      cityName: 'K'
    }, {
      cityName: 'L'
    }, {
      cityName: 'M'
    }, {
      cityName: 'N'
    }, {
      cityName: 'P'
    }, {
      cityName: 'Q'
    }, {
      cityName: 'R'
    }, {
      cityName: 'S'
    }, {
      cityName: 'T'
    }, {
      cityName: 'W'
    }, {
      cityName: 'X'
    }, {
      cityName: 'Y'
    }, {
      cityName: 'Z'
    }, ],
    citys: [],
    firstLetter: "A",
    hidden: true,
    scrollTopId: "",
    cityName: "",
    ak: "**********"
  },
  tosearchCity() {
    wx.navigateTo({
      url: "/pages/searchcity/searchcity"
    })
  },
  getPy(e) {
    console.log(e);
    this.setData({
      firstLetter: e.target.id,
      hidden: false,
    })
  },
  setPy(e) {
    console.log(111)
    this.setData({
      scrollTopId: this.data.firstLetter,
    })
    setTimeout(() => {
      this.setData({
        hidden: true,
      })
    }, 500)
  },
  setPosition(e) {
    let y = e.touches[0].clientY,
      offsettop = e.currentTarget.offsetTop,
      that = this;
    //判断选择区域,只有在选择区才会生效
    if (y > offsettop) {
      let num = parseInt((y - offsettop) / 20);
      if (num > 0 && num < 23) {
        this.setData({
          firstLetter: that.data.cityAZ[num - 1].cityName
        })
      } else if (num == 0) {
        this.setData({
          firstLetter: 'Top'
        })
      }
    };
  },
  //滑动时
  tMove: function (e) {
    if (this.data.firstTime) {
      this.setPosition(e);
      this.setData({
        firstTime: false,
      })
      return;
    }
    if (this.data.timer) {
      return;
    }
    let timer = setTimeout(() => {
      clearTimeout(timer);
      this.setData({
        timer: null
      })
      this.setPosition(e);
    }, 60)
    this.setData({
      timer
    })

  },
  // 点击城市
  selCity(e) {
    console.log(e.currentTarget.dataset.val);
    // console.log(this.data.citys);
    let app = getApp();
    app.globalData.cityName = e.currentTarget.dataset.val + '市';
    this.setData({
      cityName: e.currentTarget.dataset.val  + '市'
    })
    console.log(app.globalData);
    // wx.switchTab({
    //   url: '/pages/financingapply/financingapply',
    // })
    wx.navigateBack({
      delta: -1
    })
  },

  getPosi() {
    this.getLocation()
    let app = getApp();
    app.globalData.cityName = this.data.cityName;
    wx.switchTab({
      url: '/pages/financingapply/financingapply',
    })
  },
  getLocation() {
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success(pos) {
        console.log(pos);
        getApp().globalData.request("/coordinate/getCityNameByCoordinate", {
          lng: pos.longitude,
          lat: pos.latitude
        }, "POST").then(res => {
          console.log(res);
          if (res.code == 1) {
            that.setData({
              cityName: res.data.cityName
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLocation()
    let app = getApp();
    let cityName = app.globalData.cityName;
    this.setData({
      cityName,
      citys: citylist.citys
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getLocation()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})